/*
 * sparser.c
 *
 *  Created on: 2013-5-20
 *      Author: xkaying
 */

#include "../typedef/token.h"
#include "../parser/lexer.h"
#include "../typedef/prority.h"

#include "spackage.h"

#include <assert.h>

typedef struct super_parser
{
	int x;
} SuperParser;

/*////////////*
 //各个阶段状态含义
 * 0-3 package解析
 * 	3  解析packagecontext
 * 	3-5 packagecontext接口解析
 * 	6-7 解析import
 * 	8-13 解析enum
 *      14-18  解析interface
 *


 ***************************/
#define StatePackage 3
#define StateImport 6
#define StateEnum 8
#define StateInterface 14
#define StateClass 0
#define StateTemplate 19
#define StateSuper 0
#define StateFunctionDeclare 0
#define StateOperatorDeclare 0
#define StateTemplateParam 0

void *SParser(Lexer *lexer)
{

	assert(lexer);

	Stack *pstack;
	pstack = StackInit();

	Token *ptoken;
	SuperParser *parser;
	//包定义
	SPackage *package;
	package = 0;
	SPackageName *ppname;
	ppname = 0;
	///////////////////
	SEnum *penum;
	penum = 0;
	int ttype;
	ttype = 0;
	SInterface *pinterface;
	pinterface = 0;
	SClass *pclass;
	pclass = 0;

	int prestate;
	int state;
	state = 0;
	prestate = 0;

	int prority;
	prority = Prority_Protected;

	ptoken = LexerGetToken(lexer);
	while (ptoken) {
		switch (state)
		{
			case 0: {
				switch (ptoken->type)
				{
					case TokenKey_PACKAGE: {
						if (package) {
							printf("系统内部错误.package不为0.\n");
							return 0;
						}
						package = SPackageInit();
						state = 1;
						break;
					}
					default: {
						printf("请先进行包定义.\n");
						return 0;
					}
				}
				break;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						SPackagePush(package, ptoken->name);
						state = 2;
						break;
					}
					default: {
						printf("第%d行:错误的包名称定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_Get: {
						state = 1;
						break;
					}
					case TokenType_BlockStart: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_End: {
						//读取结束处理
						SParserPush(parser, package);
						package = 0;
						state = 0;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的包名称定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 3: {
				switch (ptoken->type)
				{
					case TokenType_End: {
						break;
					}
					case TokenKey_IMPORT: {
						if (ppname) {
							printf("系统内部错误.ppname不为0.\n");
							return 0;
						}
						ppname = SPackageNameInit();
						state = StateImport;
						break;
					}
					case TokenKey_PUBLIC: {
						prority = Prority_Public;
						state = 4;
						break;
					}
					case TokenKey_PRIVATE: {
						prority = Prority_Private;
						state = 4;
						break;
					}
					case TokenKey_PROTECTED: {
						prority = Prority_Protected;
						state = 4;
						break;
					}
						//7-12
					case TokenKey_ENUM: {
						state = StateEnum;
						if (penum) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						penum = SEnumInit(Prority_Protected);
						break;
					}
					case TokenKey_INTERFACE: {
						state = StateInterface;
						if (pinterface) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						pinterface = SInterfaceInit(Prority_Protected);
						break;
					}
					case TokenKey_CLASS: {
						state = StateClass;
						if (pclass) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						pclass = SClassInit(Prority_Protected, 0);
						break;
					}
					case TokenKey_FINAL: {
						prority = Prority_Protected;
						state = 5;
						break;
					}
					case TokenType_BlockEnd: {
						//结束处理
						SParserPush(parser, package);
						package = 0;
						state = 0;
						break;
					}
					default: {
						printf("第%d行:错误的类型定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 4: {
				switch (ptoken->type)
				{
					//7-12
					case TokenKey_ENUM: {
						state = StateEnum;
						if (penum) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						penum = SEnumInit(prority);
						break;
					}
					case TokenKey_INTERFACE: {
						state = StateInterface;
						if (pinterface) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						pinterface = SInterfaceInit(prority);
						break;
					}
					case TokenKey_CLASS: {
						state = StateClass;
						if (pclass) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						pclass = SClassInit(prority, 0);
						break;
					}
					case TokenKey_FINAL: {
						state = 5;
						break;
					}
					default: {
						printf("第%d行:错误的类型定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 5: {
				switch (ptoken->type)
				{
					case TokenKey_CLASS: {
						state = StateClass;
						if (pclass) {
							printf("系统内部错误.penum不为0.\n");
							return 0;
						}
						pclass = SClassInit(prority, 0);
						break;
					}
					default: {
						printf("第%d行:错误的包名称定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
				//////////////////////import 语句
			case 6: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						SPackageNamePush(ppname, ptoken->name);
						state = 7;
						break;
					}
					default: {
						printf("第%d行:错误的包名称定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 7: {
				switch (ptoken->type)
				{
					case TokenType_Get: {
						state = 6;
						break;
					}
					case TokenType_End: {
						SPackagePushImport(package, ppname);
						ppname = 0;
						state = StatePackage;
						break;
					}
					default: {
						printf("第%d行:错误的包名称定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
///////////////////////////////////////////////////////////////////////////////////////////////////////
				//枚举定义////////////////////////////////////////////////////
			case 8: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 9;
						SEnumAppend(penum, ptoken->name);
						break;
					}
					default: {
						printf("第%d行:错误的枚举定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 9: {
				switch (ptoken->type)
				{
					case TokenType_BlockStart: {
						state = 10;
						break;
					}
					case TokenType_End: {
						SPackagePushEnum(package, penum);
						penum = 0;
						state = StatePackage;
						break;
					}
					default: {
						printf("第%d行:错误的枚举定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 10: {
				switch (ptoken->type)
				{
					case TokenType_BlockEnd: {
						SPackagePushEnum(package, penum);
						penum = 0;
						state = StatePackage;
						break;
					}
					case TokenType_End: {
						break;
					}
					case TokenType_Variable: {
						SEnumItemName(penum, ptoken->name);
						state = 11;
						break;
					}
					default: {
						printf("第%d行:错误的枚举数据项定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 11: {
				switch (ptoken->type)
				{
					case TokenType_End: {
						state = 10;
						break;
					}
					case TokenType_Equl: {
						state = 12;
						break;
					}
					default: {
						printf("第%d行:错误的枚举数据项定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 12: {
				switch (ptoken->type)
				{
					case TokenType_Decimal: {
						state = 13;
						break;
					}
					default: {
						printf("第%d行:错误的枚举数据项定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 13: {
				switch (ptoken->type)
				{
					case TokenType_End: {
						state = 10;
						break;
					}
					default: {
						printf("第%d行:错误的枚举数据项定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
/////////////////////////////////////////////////////////////////////////////////
				/////////////interface 读取计划
			case 14: {
				switch (ptoken->type)
				{
					//读取模板 模板id
					case TokenType_Less: {
						//
						state = StateTemplate;
						StackPushInt(pstack, 17);
						break;
					}
					case TokenType_Variable: {
						SInterfaceAppend(pinterface, ptoken->name);
						state = 15;
						break;
					}
					default: {
						printf("第%d行:错误的接口定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 15: {
				switch (ptoken->type)
				{
					//读取父类接口或者类
					case TokenType_Extend: {
						//
						state = StateSuper;
						StackPushInt(pstack, 18);
						break;
					}
					case TokenType_End: {
						SPackagePushInterface(package, pinterface);
						state = StatePackage;
						break;
					}
					case TokenType_BracketStart: {
						state = 16;
						break;
					}
					default: {
						printf("第%d行:错误的接口定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 16: {
				switch (ptoken->type)
				{
					case TokenType_BracketEnd: {
						SPackagePushInterface(package, pinterface);
						state = StatePackage;
						break;
					}
					case TokenType_End: {
						break;
					}
					case TokenKey_FUNCTION: {
						state = StateFunctionDeclare;
						break;
					}

					case TokenKey_OPERATOR: {
						state = StateOperatorDeclare;
						break;
					}
					default: {
						printf("第%d行:错误的接口数据项定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
				//模板返回点
			case 17: {
				SInterfaceAppendTemplate(pinterface, ptemplate);
				ptemplate = 0;
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						SInterfaceAppend(pinterface, ptoken->name);
						state = 15;
						break;
					}
					default: {
						printf("第%d行:错误的接口定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
				//继承返回点
			case 18: {
				SInterfaceAppendSuper(pinterface, psuper);
				psuper = 0;
				switch (ptoken->type)
				{
					case TokenType_End: {
						SPackagePushInterface(package, pinterface);
						state = StatePackage;
						break;
					}
					case TokenType_BracketStart: {
						state = 16;
						break;
					}
					default: {
						printf("第%d行:错误的接口定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
////////////////////////////////////////////////////////////////
				//template 读取计划
			case 19: {
				switch (ptoken->type)
				{
					case TokenType_More: {
						//表示读取结束.
						state = StackPopInt(pstack);
						if (state == 0) {
							printf("错误的state.系统错误.\n");
							return 0;
						}
						break;
					}
					case TokenKey_CLASS:{
						//跳到读取declare阶段,区别是这个declare为0
						if(pdeclare!=0){
							printf("错误的pdeclare.系统错误.\n");
														return 0;
						}
						pdeclare=SDeclareInit();
						state=StateDeclare;
					}
					case TokenType_Variable: {
						//跳到读取declare阶段,并初始化一个declare
						state = StateDeclare;
						break;
					}
					default: {
						printf("第%d行:错误的模板定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 20: {
				switch (ptoken->type)
				{
					case TokenType_Less: {
						//表示读取结束.
						state = StateTemplateParam;
						StackPush(pstack, 21);
						break;
					}
					case TokenType_BlockStart: {
						state = 22;
						break;
					}
					case TokenType_Variable: {
						state = 22;
						break;
					}
					default: {
						printf("第%d行:错误的模板定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}
			case 21: {

				switch (ptoken->type)
				{
					case TokenType_BlockStart: {
						state = 22;
						break;
					}
					case TokenType_Variable: {
						state = 22;
						break;
					}
					default: {
						printf("第%d行:错误的模板定义.\n", ptoken->line);
						return 0;
					}
				}
				break;
			}

			default: {
				printf("状态机错误.状态(default):%d.\n", state);
				return 0;
			}
		}
		ptoken = LexerGetToken(lexer);
	}
	return parser;
}
